package com.drondea.testclient.handler;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.drondea.testclient.panel.AbstractTcpPanel;
import com.drondea.testclient.util.DateTime;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Date;

/**
 * @author shd
 * @date 2022/7/19 14:29
 */
public abstract class AbstractSubmitResponseHandler<IMessage> extends SimpleChannelInboundHandler<IMessage> {

    /**
     * 设置结束时间、计算平均提交速度
     * @param tcpPanel
     * @param sendCount
     */
    public void calAvgSubmitSpeed(AbstractTcpPanel tcpPanel, long sendCount) {
        String endTimeStr = DateTime.getString(new Date(), DateTime.Y_M_D_H_M_S_S_1);
        tcpPanel.endTimeLabel.setText(StrFormatter.format("结束时间: {}", endTimeStr));
        int secondsBetween = DateTime.between(tcpPanel.startTimeLabel.getText().split("[:：]", 2)[1].trim(), endTimeStr);
        secondsBetween = secondsBetween == 0 ? 1 : secondsBetween;
        int avgSubmitSpeed = (int) (((float) sendCount / secondsBetween) * 1000);
        tcpPanel.avgSubmitSpeedLabel.setText(StrUtil.format("平均提交速度: {}/s", avgSubmitSpeed));
    }
    
    
}
